<script>
const app = getApp();
import { 
	queryGoodsDetail //获取商品详情
} from "@/lib/api/goodsServer/goods";
import { 
	updateCart  //更新购物车商品数量
} from "@/lib/api/interestedServer/cart";
/** storage beagin */
import { 
	getLoginInfo //
} from "@/lib/storage";
import { mapState } from "vuex";
/** storage end */
import loginMixin from "@/mixins/login";

export default {
	mixins: [loginMixin],
	data() {
		return {
			specGoods: {},  // 传入到选择规格弹层组件的商品信息数据
			specClass: "none", // 切换显示选择规格弹层
			actionBuyType: null, // 0: 添加购物车; 1: 立即购买; 2: 改变当前购物车商品规格 3: 单独购买 4: 活动商品
			specGoodsData: {},
			/* 
				// 根据状态值展示商品价格 1101: 普通商品 2101: 活动商品
			*/
			showPriceStatus: "1101", 
			isRequestSpecGoods: false // 是请求接口获取商品详情
		};
	},

	methods: {
		/** 弹出层操作事件 begin */
		/**
		 * 加入购物车
		 * @param { Object } buyData // 加入购物车需要的参数
		 */
		async onAddGoodsToCart(buyData) {
			try {
				const res = await updateCart(buyData);
				this.$api.showToast("成功添加", { icon: "" });
				this.getCartCount();
				this.toggleSpec();
			} catch (error) {
				console.log("error", error);
				this.$api.showToast(error.resultInfo || "操作失败");
			}
		},

		/**
		 * 立即购买
		 * @param { Object } buyData // ）（立即购买）订单结算需要的参数
		 */
		onNowBuy(buyData) {
			buyData = JSON.stringify(buyData);
			uni.navigateTo({
				url: `/pages/user/pages/order/createOrder?buyData=${buyData}`
			});
			this.toggleSpec();
		},

		//规格弹窗开关
		toggleSpec() {
			if (this.specClass === "show") {
				this.specClass = "hide";
				setTimeout(() => {
					this.specClass = "none";
				}, 250);
			} else if (this.specClass === "none") {
				this.specClass = "show";
			}
		},

		/** 弹出层操作事件 end */

		async loginShowSpecPop(e) {
			try {
				await this.login(e);
				await this.getSpecGoods(this.goodsId, this.isRequestSpecGoods);
				this.toggleSpec();
			} catch (error) {
				this.$api.showToast(error.resultInfo || "操作失败！");
			}
		},

		/**
		 * 点击商品详情页底部按钮
		 * @parm { String } goodsId
		 * @parm { Number } actionBuyType
		 */
		async showSpecPop(goodsId, activityType, actionBuyType) {
			try {
				// this.actionBuyType = actionBuyType;
				// this.showPriceStatus = showPriceStatus;
				this.readyShowSpec(goodsId, activityType, actionBuyType);
				await this.getSpecGoods(this.goodsId, this.isRequestSpecGoods);
				this.toggleSpec();
			} catch (error) {
				console.log("error", error);
				this.$api.showToast(error.resultInfo || "操作失败！");
			}
		},

		// 根据商品类型保存商品id和弹出框展示类型
		readyShowSpec(goodsId, activityType, actionBuyType) {
			let showPriceStatus = "1101";
			switch (actionBuyType) {
				case 0:
					if (activityType === 30) {
						showPriceStatus = "2101";
					}
					break;

				case 1:
					if (activityType === 30) {
						showPriceStatus = "2101";
					}
					break;

				case 4:
					showPriceStatus = "2101";
					break;

				default:
					break;
			}
			this.saveReadyShowSpec(goodsId, actionBuyType, showPriceStatus);
		},

		// 保存商品id和操作类型
		saveReadyShowSpec(goodsId, actionBuyType, showPriceStatus) {
			this.actionBuyType = actionBuyType;
			this.showPriceStatus = showPriceStatus;
			this.goodsId = goodsId;
		},

		// 查询商品详情
		async getSpecGoods(goodsId, isRequestSpecGoods = false) {
			if (isRequestSpecGoods) {
				const propsData = {
					goodsId: goodsId
				};
				const { goodsDetail } = await queryGoodsDetail(propsData);
			}

			const specGoods = (this.specGoods =
				(isRequestSpecGoods && goodsDetail) ||
				JSON.parse(JSON.stringify(this.goodsDetail)));

			specGoods.imagePath = this.baseFileUri + specGoods.imagePathMain[0];
			specGoods.isMoreSpec = !!specGoods.specsKeyList.length;
			specGoods.skuId = specGoods.skuList[0].skuId;

			/** 处理价格 begin */
			const currentPrice = this.handleCurrentPrice(
				specGoods,
				this.showPriceStatus,
				isRequestSpecGoods
			);
			specGoods.minPrice = currentPrice.minPrice;
			specGoods.maxPrice = currentPrice.maxPrice;
			/** 处理价格 end */
		},

		/**
		 * 处理当前商品展示价格
		 * @param { Object } specGoods // 当前商品
		 * @param { String } showPriceStatus // 商品价格展示状态
		 * @param { Boolean } isRequestSpecGoods // 是否是请求接口的数据
		 */
		handleCurrentPrice(specGoods, showPriceStatus, isRequestSpecGoods) {
			const price = {
				minPrice: [],
				maxPrice: []
			};

			switch (showPriceStatus) {
				// 活动商品
				case "2101":
					if (!isRequestSpecGoods) {
						price.minPrice = specGoods.minActivityPrice;
						specGoods.maxActivityPrice &&
							(price.maxPrice = specGoods.maxActivityPrice);
					} else {
						price.minPrice = `${specGoods.minActivityPrice}`.split(
							"."
						);
						specGoods.maxActivityPrice &&
							(price.maxPrice = `${specGoods.maxActivityPrice}`.split(
								"."
							));
					}

					break;
				// 普通商品
				default:
					if (!isRequestSpecGoods) {
						price.minPrice = specGoods.minPrice;
						specGoods.maxPrice &&
							(price.maxPrice = specGoods.maxPrice);
					} else {
						price.minPrice = `${specGoods.minPrice}`.split(".");
						specGoods.maxPrice &&
							(price.maxPrice = `${specGoods.maxPrice}`.split(
								"."
							));
					}
					break;
			}

			return price;
		}
	}
};
</script>